<template>
  <div>
    <el-row>
      <el-button
        icon="el-icon-refresh"
        type="primary"
        plain
        size="mini"
        @click="openData()"
        >刷新数据</el-button
      >
      <el-button
        icon="el-icon-circle-plus-outline"
        type="warning"
        plain
        size="mini"
        @click="onAdd()"
        :disabled="!zIsOpenData"
        >增加用户</el-button
      >
      <el-dialog
        v-model="FIsShowAdminEdit"
        width="800PX"
        center
        destroy-on-close
        title="用户编辑"
      >
        <TFormAdminEdit
          :zContextParams="contextParams"
          :zContextOnCall="onCallBack"
        ></TFormAdminEdit>
      </el-dialog>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-table
          :data="FCompanyList"
          style="width: 100%; margin-top: 10px; margin-bottom: 20px"
          row-key="FCompanyID"
          border
          default-expand-all
          highlight-current-row
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          @row-click="onCompanyFitler"
        >
          <el-table-column prop="FCompanyName" label="组织结构">
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="18">
        <el-table
          :data="FAdminList"
          style="width: 100%; margin-top: 10px; margin-bottom: 20px"
          row-key="FCompanyID"
          border
          highlight-current-row
        >
          <el-table-column prop="FAdminCode" label="登陆账号">
          </el-table-column>
          <el-table-column prop="FAdminName" label="用户名称">
          </el-table-column>
          <el-table-column prop="FAdminTel" label="用户电话"></el-table-column>
          <el-table-column prop="FMyCompanyName" label="所属公司">
          </el-table-column>
          <el-table-column prop="FMyDepartmentName" label="所属部门">
          </el-table-column>
          <el-table-column prop="FRoleName" label="所属角色"> </el-table-column>
          <el-table-column prop="FStatusCode" label="状态"> </el-table-column>
          <el-table-column label="操作" width="200px">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                size="mini"
                @click="onEdit(scope.$index, scope.row)"
                >用户编辑</el-button
              >
              <el-button
                type="waring"
                plain
                size="mini"
                @click="onPersonRightEdit(scope.$index, scope.row)"
                >个人权限</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script  lang="ts">
import { Options, Vue } from "vue-class-component";
import TFormBase from "@/views/BaseForm";
import { TBaseAdmin, TBaseCompany } from "@/baselib/right/BaseRightClass";
import TFormAdminEdit from "./AdminEdit.vue";
import { IMenuItem } from "@/store";
//配合着用
@Options({
  name: "TFormAdminList",
  components: {
    TFormAdminEdit,
  },
  computed: {},
  data() {},
})
export default class TFormAdminList extends TFormBase {
  private FIsShowAdminEdit: boolean = false;
  private FIndexAdmin: TBaseAdmin = new TBaseAdmin();
  private FAdminList: TBaseAdmin[] = [];
  private FAdminSourceList: TBaseAdmin[] = [];
  private FCompanyList: TBaseCompany[] = [];
  private get contextParams() {
    return { IndexAdmin: this.FIndexAdmin };
  }
  public created() {
    this.openCompanyList();
    this.openData();
  }
  //打开数据
  public async openData() {
    this.zIsOpenData = false;
    let lResult = await this.$myBaseRightAPI.getAdminList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.FAdminList = lResult.resultData;
    if (this.FAdminList.length > 0) {
      if (this.$myStrHelper.stringIsEmpty(this.FAdminList[0].FAdminID)) {
        this.$myMsgHelp.msgDialog("返回的数据格式和前端的类定义不一至,请检查");
        return;
      }
    }
    this.FAdminSourceList = this.FAdminList;
    this.zIsOpenData = true;
    this.$myMsgHelp.msgHintAutoClose(
      "获取数据成功",
      this.$myMsgHelp.msgSuccess
    );
  }
  public async openCompanyList() {
    let lResult = await this.$myBaseRightAPI.getCompanyList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.FCompanyList = lResult.resultData;
  }
  //编辑界面回调列表事件
  private onCallBack(qAction: string, qCompany: TBaseAdmin) {
    if (qAction == "close") {
      this.FIsShowAdminEdit = false;
    }
    if (qAction == "save") {
      this.FIsShowAdminEdit = false;
      //重新刷新数据
      this.openData();
    }
  }
  //增加结构类型
  private onAdd() {
    this.FIndexAdmin = new TBaseAdmin();
    this.FIsShowAdminEdit = true;
  }
  private onEdit(qIndex: number, qCompany: TBaseAdmin) {
    this.FIndexAdmin = qCompany;
    this.FIsShowAdminEdit = true;
  }
  private onPersonRightEdit(qIndex: number, qCompany: TBaseAdmin) {
    let lTag: IMenuItem = {
      menuIndexName: "TFormPersonRightEdit-" + qCompany.FAdminID,
      menuComponent: "TFormPersonRightEdit",
      menuTitle: "个人权限_" + qCompany.FAdminName,
      menuIcon: "",
      menuParams: { admin: qCompany }, //参数
      menuChildren: [],
    };
    this.$myEvenBus.addTag(lTag);
  }
  private onCompanyFitler(qCompany: TBaseCompany) {
    let lTreeCode = "|" + qCompany.FTreeCode + ".";
    this.FAdminList = this.FAdminSourceList.filter((p) =>
      ("|" + p.FCompanyTreeCode + ".").startsWith(lTreeCode)
    );
  }
}
</script>